
import emitter from '@/utils/emitter'
import { v4 as uuidv4 } from 'uuid'

class DeviceConnectEventService {
    constructor() {
        emitter.on('device-connect-status-change-event', (data) => {
            this.handleDeviceConnectStatusChanged(data)
        })
    }
    
    /**
     * 处理设备链接状态变更回调
     * 记录设备链接变更事件
     */
    handleDeviceConnectStatusChanged(data) {
        console.log('收到设备链接更改事件:', data)
        const uuid = uuidv4();
        let deviceConnectEventStore = useDeviceConnectEventStore()
        deviceConnectEventStore.addNewEvent({
            id: uuid.replace(/-/g, ''), // UUID事件ID
            sn: data.sn, // 设备序列号
            address: data.address, // 设备IP+端口
            type: data.type, // 类型，connected-连接，disconnected-断开连接
            reason: '', // 设备断开链接的原因，目前一般为空
            timestamp: Date.now(), // 时间戳，毫秒
        })
        // console.log("device connect event list:", deviceConnectEventStore.eventList)
    }
}

export const deviceConnectEventService = new DeviceConnectEventService()